//
//  MineOrderSegView.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/14.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "MineOrderSegView.h"

@interface MineOrderSegView ()

@property (nonatomic, strong) UIButton *button1;
@property (nonatomic, strong) UIButton *button2;
@property (nonatomic, strong) UIButton *button3;
@property (nonatomic, strong) UIButton *button4;

@property (nonatomic, strong) UIView *line;

@end

@implementation MineOrderSegView


- (void)setupSubViews
{
    self.button1 = [[UIButton alloc] init];
    [self.button1 setTitle:@"全部" forState:UIControlStateNormal];
    [self.button1 setTitleColor:kColor333333 forState:UIControlStateNormal];
    [self.button1 setTitleColor:kColor287AF6 forState:UIControlStateSelected];
    self.button1.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [self addSubview:self.button1];
    self.button1.tag = 0;
    self.button1.selected = YES;
    
    self.button2 = [[UIButton alloc] init];
    [self.button2 setTitle:@"待付款" forState:UIControlStateNormal];
    [self.button2 setTitleColor:kColor333333 forState:UIControlStateNormal];
    [self.button2 setTitleColor:kColor287AF6 forState:UIControlStateSelected];
    self.button2.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [self addSubview:self.button2];
    self.button2.tag = 1;
    
    self.button3 = [[UIButton alloc] init];
    [self.button3 setTitle:@"待收货" forState:UIControlStateNormal];
    [self.button3 setTitleColor:kColor333333 forState:UIControlStateNormal];
    [self.button3 setTitleColor:kColor287AF6 forState:UIControlStateSelected];
    self.button3.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [self addSubview:self.button3];
    self.button3.tag = 2;
    
    self.button4 = [[UIButton alloc] init];
    [self.button4 setTitle:@"已完成" forState:UIControlStateNormal];
    [self.button4 setTitleColor:kColor333333 forState:UIControlStateNormal];
    [self.button4 setTitleColor:kColor287AF6 forState:UIControlStateSelected];
    self.button4.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [self addSubview:self.button4];
    self.button4.tag = 3;
    
    CGFloat w = 40;
    self.line = [[UIView alloc] initWithFrame:CGRectMake((kScreenW/4-w)/2, 42, w, 2)];
    self.line.backgroundColor = kColor287AF6;
    [self addSubview:self.line];
    
    [self.button1 addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.button2 addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.button3 addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.button4 addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setupConstraints
{
    [self.button1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self);
    }];
    
    [self.button2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.button1.mas_right);
        make.top.bottom.equalTo(self);
        make.width.equalTo(self.button1);
    }];
    
    [self.button3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.button2.mas_right);
        make.top.bottom.equalTo(self);
        make.width.equalTo(self.button1);
    }];
    
    [self.button4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.button3.mas_right);
        make.top.bottom.equalTo(self);
        make.right.equalTo(self);
        make.width.equalTo(self.button1);
    }];
    
}

- (void)setOffSet:(CGFloat)offSet
{
    _offSet = offSet;
    CGFloat w = 40;
    CGFloat start  = CGRectGetMidX(self.button1.frame);
    CGFloat end = CGRectGetMidX(self.button4.frame);
    CGFloat x = (end-start)*(offSet/(3*kScreenW))+(kScreenW/4-w)/2;
    self.line.frame = CGRectMake(x, 42, w, 2);
    
    if (offSet < kScreenW/2) {
        self.button1.selected = YES;
        self.button2.selected = NO;
        self.button3.selected = NO;
        self.button4.selected = NO;
    }
    else if (offSet < kScreenW/2 + kScreenW && offSet >= kScreenW/2) {
        self.button1.selected = NO;
        self.button2.selected = YES;
        self.button3.selected = NO;
        self.button4.selected = NO;
    }
    else if (offSet < kScreenW/2 + kScreenW*2 && offSet >= kScreenW/2 + kScreenW) {
        self.button1.selected = NO;
        self.button2.selected = NO;
        self.button3.selected = YES;
        self.button4.selected = NO;
    }
    else {
        self.button1.selected = NO;
        self.button2.selected = NO;
        self.button3.selected = NO;
        self.button4.selected = YES;
    }
}

- (void)clickAction:(UIButton *)sender
{
    if (!self.scrollView.isTracking && !self.scrollView.isDecelerating) {
        if (self.selectIndex) {
            self.selectIndex(sender.tag);
        }
    }
}

@end
